﻿package mortal.game.view.copy.defenseCopy.view
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import __AS3__.vec.*;
    import com.gengine.global.*;
    import com.gengine.resource.*;
    import com.gengine.utils.*;
    import com.mui.containers.globalVariable.*;
    import com.mui.controls.*;
    import extend.language.*;
    import fl.data.*;
    import fl.events.*;
    import flash.events.*;
    import flash.geom.*;
    import mortal.common.global.*;
    import mortal.component.gComplexList.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.item.*;
    import mortal.game.view.common.pageSelect.*;
    import mortal.game.view.common.util.*;
    import mortal.game.view.copy.defenseCopy.*;
    import mortal.game.view.copy.escort.renderer.*;
    import mortal.game.view.interfaceCallModule.*;
    import mortal.game.view.skill.*;
    import mortal.mvc.core.*;

    public class DefenseCopyShopEquipView extends GSprite
    {
        private var _categoryList:GComplexList;
        private var _canComList:GTileList;
        private var _tool:SpecialPageTool;
        private var _advanceBtn:GButton;
        private var _retreatBtn:GButton;
        private var _composeView:DefenseCopyComposeView;
        private var _buyBtn:GButton;
        private var _baseItem:BaseItem;
        private var _name:GTextFiled;
        private var _price:GTextFiled;
        private var _equipMoney:GTextFiled;
        private var _attr:GTextFiled;
        private var _skillExplain:GTextArea;
        private var _moneyBg:GBitmap;
        private var _moneyBg2:GBitmap;
        private var _itemCode:int;
        private static var _instance:DefenseCopyShopEquipView;

        public function DefenseCopyShopEquipView()
        {
            this.isHideDispose = true;
            return;
        }// end function

        private function onChange(event:Event) : void
        {
            this._categoryList.unfold.data = this._categoryList.titleData[this._categoryList.selectIndex];
            this._categoryList.drawNow();
            return;
        }// end function

        private function onItemClick(event:ListEvent) : void
        {
            var _loc_2:* = event.item as ItemData;
            Dispatcher.dispatchEvent(new DataEvent(EventName.DefenseCopyComposeUpdate, _loc_2.itemCode));
            return;
        }// end function

        private function onAdvance(event:MouseEvent) : void
        {
            this.canComListData = this._tool.advance();
            return;
        }// end function

        private function onRetreat(event:MouseEvent) : void
        {
            this.canComListData = this._tool.retreat();
            return;
        }// end function

        private function onComposeUpdate(event:DataEvent) : void
        {
            UITrackPanelSub2.ckArr = [];
            this._itemCode = event.data as int;
            if (this._composeView)
            {
                this._composeView.dispose();
            }
            Cache.instance.copy.defenseCopy.maxLayer = 0;
            this._composeView = UIFactory.getUICompoment(DefenseCopyComposeView, 548, 100, this);
            this._composeView.setItemCode(event.data as int);
            DefenseCopyUtil.setMaxLenght();
            this._composeView.updatePos();
            this._composeView.updateAllPackHasItem();
            var _loc_2:* = new ItemData(this._itemCode);
            this._baseItem.itemData = _loc_2;
            this._name.htmlText = HTMLUtil.addColor(_loc_2.name, ColorConfig.instance.getItemColorString(_loc_2.itemInfo.color));
            this.updatePriceTxt();
            this._tool.data = DefenseCopyComposeConfig.instance.findIncludItemCodeConfigArr(this._itemCode);
            this.canComListData = this._tool.showData;
            this.attr = _loc_2;
            this.skillExplain = _loc_2;
            var _loc_3:* = this._tool.data.length > 5;
            this._retreatBtn.visible = this._tool.data.length > 5;
            this._advanceBtn.visible = _loc_3;
            return;
        }// end function

        private function updatePriceTxt() : void
        {
            var _loc_1:int = 0;
            var _loc_2:int = 0;
            var _loc_3:String = null;
            if (this._itemCode > 0)
            {
                _loc_1 = DefenseCopyUtil.getPriceByCode(this._itemCode);
                _loc_2 = Cache.instance.copy.defenseCopy.copyReward.equipPoint;
                _loc_3 = _loc_2 >= _loc_1 ? (GlobalStyle.colorBai) : (GlobalStyle.colorHong);
                this._price.htmlText = HTMLUtil.addColor(_loc_1.toString(), _loc_3);
            }
            return;
        }// end function

        private function set attr(param1:ItemData) : void
        {
            var _loc_4:AttributeData = null;
            if (param1.itemInfo is ItemEquipInfo == false)
            {
                this._attr.htmlText = "";
                return;
            }
            var _loc_2:* = AttributeUtil.getEquipmentBasicAttrs(param1.itemInfo as ItemEquipInfo, param1.addPercent);
            var _loc_3:String = "";
            for each (_loc_4 in _loc_2)
            {
                
                _loc_3 = _loc_3 + ("+" + _loc_4.value + _loc_4.name + "  ");
            }
            this._attr.htmlText = _loc_3;
            return;
        }// end function

        private function set skillExplain(param1:ItemData) : void
        {
            var _loc_3:Array = null;
            var _loc_4:TSkill = null;
            var _loc_5:String = null;
            var _loc_6:int = 0;
            var _loc_2:String = "";
            if (param1.itemInfo is ItemEquipInfo && param1.itemInfo.skill)
            {
                _loc_3 = param1.itemInfo.skill.split("#");
                for each (_loc_6 in _loc_3)
                {
                    
                    _loc_4 = SkillConfig.instance.getInfoById(_loc_6);
                    _loc_5 = _loc_4.name;
                    _loc_5 = _loc_5 + (_loc_4.triggerType != ESkillTriggType._ESkillTriggActive ? (Language.getString(50159)) : (Language.getString(50158)));
                    _loc_2 = _loc_2 + (HTMLUtil.addColor(_loc_5 + "：", GlobalStyle.colorHuang) + SkillUtil.alySkillDesc(_loc_4) + "<br>");
                }
            }
            this._skillExplain.htmlText = _loc_2;
            return;
        }// end function

        private function set canComListData(param1:Array) : void
        {
            var _loc_3:int = 0;
            var _loc_2:Array = [];
            for each (_loc_3 in param1)
            {
                
                _loc_2.push(new ItemData(_loc_3));
            }
            this._canComList.dataProvider = new DataProvider(_loc_2);
            this._canComList.drawNow();
            this._canComList.x = 475 + (this._canComList.width - this._canComList.dataProvider.length * this._canComList.columnWidth) / 2;
            return;
        }// end function

        private function updateMoney(param1:Object = null) : void
        {
            var _loc_2:* = Cache.instance.copy.defenseCopy.copyReward.equipPoint;
            this._equipMoney.text = Language.getStringByParam(50156, _loc_2);
            this._moneyBg.x = this._equipMoney.x + this._equipMoney.textWidth + 5;
            this.updatePriceTxt();
            if (this._composeView)
            {
                this._composeView.updatePrice();
                this._composeView.updateAllPackHasItem();
            }
            return;
        }// end function

        private function onBagUpdte(param1:Object) : void
        {
            if (this._itemCode > 0)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.DefenseCopyComposeUpdate, this._itemCode));
            }
            return;
        }// end function

        private function onBuy(event:MouseEvent) : void
        {
            if (this._itemCode > 0)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.DefenseCopyShopBuy, {itemCode:this._itemCode, amount:1}));
            }
            return;
        }// end function

        public function set tabBarIndex(param1:int) : void
        {
            Cache.instance.copy.defenseCopy.curTab = param1;
            if (DefenseCopyUtil.isShowOrangeEquip())
            {
                this._categoryList.titleData = DefenseCopyUtil.isEquipTab ? ([1, 2, 3, 4]) : ([0]);
            }
            else
            {
                this._categoryList.titleData = DefenseCopyUtil.isEquipTab ? ([1, 2, 3]) : ([0]);
            }
            this._categoryList.selectIndex = 1;
            return;
        }// end function

        public function get baseItemPoint() : Point
        {
            if (this._baseItem == null)
            {
                return new Point(Global.stage.stageWidth / 2, Global.stage.stageHeight / 2);
            }
            var _loc_1:* = new Point(this._baseItem.x, this._baseItem.y);
            return this._baseItem.parent.localToGlobal(_loc_1);
        }// end function

        private function onResCompl() : void
        {
            pushUIToDisposeVec(UIFactory.bitmap(ImagesConst.DefenseCopyTxt1, 310, 30, this));
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            pushUIToDisposeVec(UIFactory.bg(255, 12, 632, 541, this));
            pushUIToDisposeVec(UIFactory.bg(425, 20, 320, 53, this));
            pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.GroupCopyDefenseBg3, 262, 79, this));
            this._categoryList = UIFactory.getUICompoment(GComplexList, 9.5, 13, this);
            this._categoryList.titleType = DefenseCopyShopNewCatogeryHead;
            this._categoryList.unfoldType = DefenseCopyUnfold;
            this._categoryList.titleH = 47;
            this._categoryList.titleGap = 2;
            this._categoryList.titleData = [0, 1, 2, 3, 4];
            this._categoryList.addEventListener(Event.CHANGE, this.onChange);
            this._categoryList.drawNow();
            this._canComList = UIFactory.tileList(475, 27, 225, 50, this);
            this._canComList.setStyle("cellRenderer", BgItemCellRenderer);
            this._canComList.columnWidth = 45;
            this._canComList.rowHeight = 45;
            this._canComList.direction = GBoxDirection.HORIZONTAL;
            this._canComList.configEventListener(ListEvent.ITEM_CLICK, this.onItemClick);
            this._advanceBtn = UIFactory.gButton("", 435, 37, 22, 22, this, "PrevPageButton");
            this._retreatBtn = UIFactory.gButton("", 714, 37, 22, 22, this, "NextPageButton");
            this._tool = new SpecialPageTool();
            this._tool.maxLenght = 5;
            pushUIToDisposeVec(UIFactory.bg(260, 443, 625, 1, this, ImagesConst.SplitLine));
            pushUIToDisposeVec(UIFactory.bg(698, 450, 1, 92, this, ImagesConst.SplitLine5));
            this._buyBtn = UIFactory.gButton("" + Language.getString(70858), 753, 480, 72, 28, this, "BrownBtn");
            this._buyBtn.configEventListener(MouseEvent.CLICK, this.onBuy);
            this._buyBtn.name = "DefenceCopyBuyEquipBtn";
            this._baseItem = UIFactory.baseItem(295, 456, 42, 42, this);
            this._baseItem.setItemStyle();
            this._name = UIFactory.gTextField("", 350, 455, 100, 20, this);
            this._price = UIFactory.gTextField("", 780, 455, 100, 20, this);
            this._equipMoney = UIFactory.gTextField("", 715, 515, 160, 24, this, GlobalStyle.textFormatPutong.setSize(14));
            this._attr = UIFactory.gTextField("", 350, 475, 348, 40, this, GlobalStyle.textFormatLv);
            this._attr.wordWrap = true;
            this._attr.multiline = true;
            this._attr.mouseEnabled = false;
            this._skillExplain = UIFactory.textArea("", 290, 500, 410, 45, this);
            this._skillExplain.setStyle("defaultTextFormat", GlobalStyle.textFormatPutong);
            this._moneyBg = UIFactory.bitmap(ImagesConst.Jinbi, 840, 516, this);
            this._moneyBg2 = UIFactory.bitmap(ImagesConst.Jinbi, 760, 455, this);
            this.updateMoney();
            this._advanceBtn.configEventListener(MouseEvent.CLICK, this.onAdvance);
            this._retreatBtn.configEventListener(MouseEvent.CLICK, this.onRetreat);
            Dispatcher.addEventListener(EventName.DefenseCopyComposeUpdate, this.onComposeUpdate);
            NetDispatcher.addCmdListener(ServerCommand.DefenseCopyRewardUpdate, this.updateMoney);
            NetDispatcher.addCmdListener(ServerCommand.DefenseCopyBagUpdate, this.onBagUpdte);
            LoaderHelp.addResCallBack(ResFileConst.defenseCopy, this.onResCompl);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this._buyBtn.name = "";
            Dispatcher.removeEventListener(EventName.DefenseCopyComposeUpdate, this.onComposeUpdate);
            NetDispatcher.removeCmdListener(ServerCommand.DefenseCopyRewardUpdate, this.updateMoney);
            NetDispatcher.removeCmdListener(ServerCommand.DefenseCopyBagUpdate, this.onBagUpdte);
            this._itemCode = 0;
            this._categoryList.dispose(param1);
            this._canComList.dispose(param1);
            this._buyBtn.dispose(param1);
            this._baseItem.dispose(param1);
            this._name.dispose(param1);
            this._price.dispose(param1);
            this._equipMoney.dispose(param1);
            this._moneyBg.dispose(param1);
            this._moneyBg2.dispose(param1);
            this._advanceBtn.dispose(param1);
            this._retreatBtn.dispose(param1);
            this._attr.dispose(param1);
            this._skillExplain.dispose(param1);
            this._categoryList = null;
            this._canComList = null;
            this._buyBtn = null;
            this._baseItem = null;
            this._name = null;
            this._price = null;
            this._equipMoney = null;
            this._moneyBg = null;
            this._moneyBg2 = null;
            this._advanceBtn = null;
            this._retreatBtn = null;
            this._attr = null;
            this._skillExplain = null;
            return;
        }// end function

        public static function get instance() : DefenseCopyShopEquipView
        {
            if (!_instance)
            {
                _instance = new DefenseCopyShopEquipView;
            }
            return _instance;
        }// end function

    }
}
